package com.edu.hre.db.sql.admin;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

import com.edu.hre.common.utils.CommonUtils;
import com.edu.hre.db.pojo.HreMajor;
import com.edu.hre.db.service.BaseService;
import com.edu.hre.db.utils.Compare;
import com.edu.hre.struts.form.admin.MajorForm;
import com.edu.hre.struts.form.admin.MajorListForm;

public class MajorDAOService implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 3394301416150421253L;
	public static final String BEAN_NAME = "majorDAOService";
	private BaseService baseService  = null;

	public void setBaseService(BaseService baseService) {
		this.baseService = baseService;
	}

	public ArrayList<MajorForm> getMajorListInfo(int limit, int offset){

		List<HreMajor> hreMajors = baseService.getPojos(null, -1, null, new String[]{"dateCreated"}, new Boolean[]{true}, limit, offset, HreMajor.class);
		ArrayList<MajorForm> majors = new ArrayList<MajorForm>();
		
		if (hreMajors == null) {
			return majors;
		}
		for (HreMajor hreMaj: hreMajors) {
			
			try {

				MajorForm major = new MajorForm();
				major.setId(hreMaj.getId() + "");
				major.setName(CommonUtils.normalizeString(hreMaj.getName()));
				
				majors.add(major);
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
		
		return majors;
	}

	public int countMajors(){

		int count = baseService.countByColumns(null, null, null, "id", HreMajor.class);
		return count < 0 ? 0 : count;
	}

	public void deleteSkills(MajorListForm form){

		ArrayList<MajorForm> info = (ArrayList<MajorForm>) form.getAllMajors();
		
		for (int i = 0; i < info.size(); i++) {
			
			try {

				MajorForm edu = info.get(i);
				if (edu.isSelected()) {
					baseService.deleteFirstPojo("id", Compare.EQUAL, Integer.parseInt(edu.getId()), null, null, 0, HreMajor.class);
				}
			} catch (Exception e) {
				CommonUtils.println(e.getMessage());
			}
		}
	}
}
